from docx import Document
from docx.shared import Cm
from PIL import Image

def insert_image_after_text(doc, text_to_find, image_path):
    for paragraph in doc.paragraphs:
        if text_to_find in paragraph.text:
            paragraph.add_run().add_break()  # Add a line break
            run = paragraph.add_run()
            run.add_picture(image_path, width=Cm(20), height=Cm(11))  # Adjust width and height as needed
            paragraph.alignment = 1  # Center alignment (0 for left, 1 for center, 2 for right)
            break

def main():
    document = Document("EASE 计算书.docx")  # Replace with the path to your Word document

    image_files = [
        ("D125.BMP", "125Hz直达声声压分布图"),
        ("D250.BMP", "250Hz直达声声压分布图"),
        ("D500.BMP", "500Hz直达声声压分布图"),
        ("D1000.BMP", "1000Hz直达声声压分布图"),
        ("D2000.BMP", "2000Hz直达声声压分布图"),
        ("D4000.BMP", "4000Hz直达声声压分布图"),
        ("D8000.BMP", "8000Hz直达声声压分布图"),
        ("DALL.BMP", "100Hz-8000Hz直达声声压分布图"),
        ("T125.BMP", "125Hz总声场声压分布图"),
        ("T250.BMP", "250Hz总声场声压分布图"),
        ("T500.BMP", "500Hz总声场声压分布图"),
        ("T1000.BMP", "1000Hz总声场声压分布图"),
        ("T2000.BMP", "2000Hz总声场声压分布图"),
        ("T4000.BMP", "4000Hz总声场声压分布图"),
        ("T8000.BMP", "8000Hz总声场声压分布图"),
        ("TALL.BMP", "100Hz-8000Hz总声场声压分布图"),
        ("STI.BMP", "快速传递指数（STI值分布图）"),
        ("ALC.BMP", "辅音清晰度损失率（ALcons%值分布图）"),
        ("QUAD.BMP", "声学模型"),
        ("AIM.BMP", "扬声器指向"),

     # Add more image-file and corresponding text pairs as needed
    ]

    for image_file, text_to_find in image_files:
        image_path = f"./{image_file}"  # Replace with the path to your image folder
        insert_image_after_text(document, text_to_find, image_path)

    document.save("EASE计算书_output.docx")  # Replace with the desired output document name

if __name__ == "__main__":
    main()
